package cn.lghuntfor.commons.sms.api.service.impl;

import cn.lghuntfor.commons.common.result.ReturnData;
import cn.lghuntfor.commons.sms.api.common.Providers;
import cn.lghuntfor.commons.sms.api.service.ISmsService;
import cn.lghuntfor.commons.sms.api.service.InheritSmsService;
import cn.lghuntfor.commons.sms.api.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;

import java.util.Map;

/**
 * 短信服务实现类
 * @author liaogang
 * @date 2021/2/19 14:39
 */
public class SmsServiceImpl implements SmsService {

    /**
     * 使用哪一家短信供应商, 可以切换
     * 查看枚举: Providers
     */
    @Value("${spring.sms.provider:}")
    private String provider;

    @Override
    public void setProvider(Providers provider) {
        this.provider = provider.name();
    }

    @Autowired
    private ApplicationContext context;

    /**
     * 获取实际的短信服务商的短信服务类
     */
    private ISmsService getSmsService() {
        return context.getBean(provider, InheritSmsService.class);
    }

    @Override
    public ReturnData<Object> sendMessage(String[] mobiles, String signature, String templateId, Map<String, String> params) {
        return getSmsService().sendMessage(mobiles, signature, templateId, params);
    }

    @Override
    public ReturnData<Object> sendOverseaMessage(String mobile, String signature, String templateId, Map<String, String> params) {
        return getSmsService().sendOverseaMessage(mobile, signature, templateId, params);
    }

    @Override
    public ReturnData<Object> sendFulltextMessage(String[] mobile, String content) {
        return getSmsService().sendFulltextMessage(mobile, content);
    }

}
